package htlgkr.nodlehs;

import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class NODLEHSActivity extends Activity {
	Button[][] table;
	String path;
	SQLiteDatabase database;
	TableRow header;
	TableRow body;
	TableLayout tl;
	TableRow edit;
	boolean editMode = false;
	private static final int REQUEST_CODE_ROOM = 111;
	private static final int REQUEST_CODE_SUBJECT = 222;
	private static final int REQUEST_CODE_UNIT = 333;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initDatabaseContent();
		
		

		View mainLayout = findViewById(R.id.mainListView);
		edit = (TableRow) findViewById(R.id.edit);
		header = (TableRow) findViewById(R.id.header);
		tl = (TableLayout) findViewById(R.id.timetable);
		mainLayout.setBackgroundColor(Color.LTGRAY);
		table = new Button[9][5];

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				createTable();
			}
		};
		handler.postDelayed(new Thread(new Runnable() {

			public void run() {
				handler.sendEmptyMessage(0);
			}
		}), 500);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	public void initDatabaseContent() {

		NodlehsDatabase bridge = new NodlehsDatabase(this);
		database = bridge.getWritableDatabase();
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent i;
		switch (item.getItemId()) {
		case R.id.men_editMode:
			enableAddRow();
			Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
			//Intent i = new Intent(this, ColorChooser.class);
			//startActivity(i);
			return true;
		case R.id.men_newSubject:
			i = new Intent(getApplicationContext(), SubjectList.class);
			startActivity(i);
			//addNewSubject();
			return true;
		case R.id.men_newTeacher:
			addNewTeacher();
			return true;
		case R.id.men_newRoom:
			i = new Intent(getApplicationContext(), RoomList.class);
			startActivity(i);
			//addNewRoom();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void enableAddRow() {
		if (edit.isShown()) {
			edit.setVisibility(View.GONE);
		} else {
			edit.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (resultCode) {
		case 111:
			Log.i("SubAct", resultCode + "");
		case 222:
			Log.i("SubAct", resultCode + "");
		case 333:
			Log.i("SubAct", resultCode + "");
		}

	}

	private void addNewRoom() {
		database.close();
		Intent newUnitInt = new Intent(this, CreateNewRoomActivity.class);
		startActivityForResult(newUnitInt, REQUEST_CODE_ROOM);
		// startActivityForResult(new Intent(this, CreateNewRoomActivity.class),
		// RESULT_OK);
	}

	public void newUnitClicked() {
		database.close();
		Intent newUnitInt = new Intent(this, CreateNewUnitActivity.class);
		startActivityForResult(newUnitInt, REQUEST_CODE_UNIT);
	}

	public void addNewTeacher() {
		database.close();
		Intent newUnitInt = new Intent(this, CreateNewTeacherActivity.class);
		startActivity(newUnitInt);
	}

	public void addNewSubject() {
		database.close();
		Intent newUnitInt = new Intent(this, CreateNewSubjectActivity.class);
		startActivityForResult(newUnitInt, REQUEST_CODE_SUBJECT);
	}

	public void createTable() {

		int x = findViewById(R.id.timetable).getWidth();
		int y = findViewById(R.id.timetable).getHeight();
		Calendar c = Calendar.getInstance();
		int today = c.get(Calendar.DAY_OF_WEEK);

		for (int i = 0; i < 5; i++) {
			TextView dayOW = new TextView(this);
			dayOW.setWidth(x / 5);
			dayOW.setGravity(Gravity.CENTER);
			dayOW.setTextColor(Color.BLACK);
			switch (i) {
			case 0:
				if (today == Calendar.MONDAY) {
					dayOW.setBackgroundColor(Color.GREEN);
				}
				dayOW.setText("MO");
				break;
			case 1:
				if (today == Calendar.TUESDAY) {
					dayOW.setBackgroundColor(Color.GREEN);
				}
				dayOW.setText("DI");
				break;
			case 2:
				if (today == Calendar.WEDNESDAY) {
					dayOW.setBackgroundColor(Color.GREEN);
				}
				dayOW.setText("MI");
				break;
			case 3:
				if (today == Calendar.THURSDAY) {
					dayOW.setBackgroundColor(Color.GREEN);
				}
				dayOW.setText("DO");
				break;
			case 4:
				if (today == Calendar.FRIDAY) {
					dayOW.setBackgroundColor(Color.GREEN);
				}
				dayOW.setText("FR");
				break;
			}
			header.addView(dayOW);
		}
		
		body = new TableRow(this);
		for(int i = 0; i <5; i++){
			Button unit = new Button(this);
			unit.setWidth(x/5);
			
		}
		
		
		for (int i = 0; i < 5; i++) {
			Button editField = new Button(this);
			editField.setText("Einheit erstellen");
			editField.setWidth(x / 5);
			editField.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					newUnitClicked();
				}
			});
			editField.setBackgroundColor(Color.GRAY);

			edit.addView(editField);
		}
		edit.setVisibility(View.GONE);

	}

	public int[] getTableCoords(int id) {
		int[] coords = new int[2];
		String s = id + "";
		coords[0] = Integer.parseInt(s.substring(0, 1));
		coords[1] = Integer.parseInt(s.substring(s.length() - 1));
		// Log.i("Coords", coords[0] + "/" +coords[1]);
		return coords;
	}
}
